--FrameRaidDetails.lua

local main = nil
--local out = nil
local raidedit = nil
local btnstart = nil
local btnend = nil
local btnlistraidmembers = nil
local editlistwindow = nil

local btnwidth = 60
local btnheight = 25
local border = 10
local txtheight = 26
local raidtxtwidth = 100

local function CreateCurrentRaidLabel(p)
	local w = raidtxtwidth;
	local h = txtheight;

	local edit = CreateFrame( "EditBox", "$parent_EditRaid", p, "InputBoxTemplate" );
	edit:SetPoint("TOPLEFT", p, "TOPLEFT", border * 2,-border);
	edit:SetWidth(w); edit:SetHeight(h);
	edit:SetText("");
	edit:SetAutoFocus(false)
	edit:SetScript("OnEnterPressed", function(self) self:ClearFocus()  end)

	return edit
end

local function CreateBtnRaidStart(p)

	local edit = CreateFrame( "Button", "$parent_StartRaid", p, "UIPanelButtonTemplate" );
	edit:SetPoint("TOPLEFT", p, "TOPLEFT", border * 3 + raidtxtwidth,-border);
	edit:SetWidth(btnwidth); edit:SetHeight(btnheight);
	edit:SetText("Start");
	edit:RegisterForClicks("LeftButtonUp");
	edit:SetScript("OnClick", function() SNFStartRaid(raidedit:GetText(), GetUnitName("player"), true)  end)

	return edit
end

local function CreateBtnRaidEnd(p)

	local edit = CreateFrame( "Button", "$parent_EndRaid", p, "UIPanelButtonTemplate" );
	edit:SetPoint("TOPLEFT", p, "TOPLEFT", border * 4 + btnwidth + raidtxtwidth,-border);
	edit:SetWidth(btnwidth); edit:SetHeight(btnheight);
	edit:SetText("End");
	edit:Disable()
	edit:RegisterForClicks("LeftButtonUp");
	edit:SetScript("OnClick", function() SNFEndRaid()  end)

	return edit
end

local function CreateBtnListMembers(p)

	local edit = CreateFrame( "Button", "$parent_ListMembers", p, "UIPanelButtonTemplate" );
	edit:SetPoint("TOPLEFT", p, "TOPLEFT", border,-(txtheight + border));
	edit:SetWidth(btnwidth * 2); edit:SetHeight(btnheight);
	edit:SetText("List Raid Start");
	edit:RegisterForClicks("LeftButtonUp");
	edit:SetScript("OnClick", function(self) SNFUIListRaidMembers(self) end)

	return edit
end

local function CreateEditListWindow(p)

	local w = p:GetWidth() - (border * 5 + btnwidth * 2 + raidtxtwidth);
	local h = p:GetHeight() - border;

	local edit = CreateFrame( "EditBox", "$parent_EditListMembers", p, "InputBoxTemplate" );
	edit:SetPoint("TOPRIGHT", p, "TOPRIGHT", -border / 2,- border);
	edit:SetPoint("BOTTOM", p, "BOTTOM",0,border);
	edit:SetMultiLine(true)
	edit:SetWidth(w); edit:SetHeight(h);
	edit:SetText("");
	edit:SetAutoFocus(false)
--	edit:SetScript("OnEnterPressed", function(self) self:ClearFocus()  end)

	return edit
end

local function CreateRaidMgmtOutputFrame(p)

	local w = p:GetWidth() - (border * 5 + btnwidth * 2 + raidtxtwidth);
	local h = p:GetHeight() - border;

	local f =CreateFrame("FRAME","SNFDKPRaidMgmt", p);
	f:SetWidth(w); f:SetHeight(h);
	f:SetPoint("TOPRIGHT",p, "TOPRIGHT", -border / 2,- border / 2);

	f:SetBackdrop({
		  bgFile="Interface\\Tooltips\\UI-Tooltip-Background",
		  edgeFile="Interface\\Tooltips\\UI-Tooltip-Border",
		  tile=1, tileSize=10, edgeSize=border,
		  insets={left=3, right=3, top=3, bottom=3}
	});

--	local fs = f:CreateFontString("$parentText","ARTWORK","GameFontNormal");
--	fs:SetAllPoints();--UnitName("player")..
--	fs:SetText("THIS IS SECOND|nRaid|nFrame");

	return f

end

local function CreateRaidMgmtFrame()

	local w = 500;
	local h = 300;

	local f=CreateFrame("FRAME","SNFDKPRaidMgmt",UIParent);
	f:SetWidth(w); f:SetHeight(h);
	f:SetPoint("CENTER",UIParent);
	f:SetMovable(1);
	f:EnableMouse(1);
	f:RegisterForDrag("LeftButton");

	f:SetScript("OnDragStart", function(self) self:StartMoving() end)
	f:SetScript("OnDragStop", function(self) self:StopMovingOrSizing() end)

	f:SetBackdrop({
		  bgFile="Interface\\Tooltips\\UI-Tooltip-Background",
		  edgeFile="Interface\\Tooltips\\UI-Tooltip-Border",
		  tile=1, tileSize=10, edgeSize=10,
		  insets={left=3, right=3, top=3, bottom=3}
	});

--	local fs = f:CreateFontString("$parentText","ARTWORK","GameFontNormal");
--	fs:SetAllPoints();--UnitName("player")..
--	fs:SetText("THIS IS A TEST|nTest2|nTest3");

	local cl = CreateFrame( "Button", "$parent_CloseButton", f, "UIPanelCloseButton" );
	cl:SetPoint("TOPRIGHT", f);

	--out = CreateRaidMgmtOutputFrame(f)

	raidedit = CreateCurrentRaidLabel(f);
	btnstart = CreateBtnRaidStart(f)
	btnend = CreateBtnRaidEnd(f)
	btnlistraidmembers = CreateBtnListMembers(f)
	editlistwindow = CreateEditListWindow(f)
	return f

end

function SNFGetRaidMgmtFrame()
	return main
end

function SNFShowRaidMgmtFrame()

	if not main then
		main = CreateRaidMgmtFrame()
	end
	if main then main:Show(); end
end

function SNFHideRaidMgmtFrame()

	if not main then
		CreateRaidMgmtFrame()
	end
	if main then main:Hide(); end
end

function SNFToggleRaidMgmtFrame()

	if not main or not main:IsVisible() then
		SNFShowRaidMgmtFrame()
	else
		SNFHideRaidMgmtFrame()
	end
end

function SNFUIUpdateRaidID(raid, raidstarted)

	if not main then
		main = CreateRaidMgmtFrame()
	end
	if not main then return end

	if not raidedit then return end

	raidedit:SetText(raid)
end

function SNFUIRaidStarted()
	if btnend then btnend:Enable() end
	if btnstart then btnstart:Disable() end
end

function SNFUIRaidEnded()
	if btnstart then btnstart:Enable() end
	if btnend then btnend:Disable() end
end

local function UIShowListWindow()
	if not editlistwindow then return end
	editlistwindow:Show()

end

function SNFUIListRaidMembers(self)

	UIShowListWindow()

	if not editlistwindow then return end

	editlistwindow:SetText("")

	local list = nil
	local list2 = nil
	if self:GetText() == "List Raid Start" then
		self:SetText("List Raid End")
		list = SNFDKP_GetRaidMembers(raidedit:GetText(), "StartList")
	elseif self:GetText() == "List Raid End" then
		self:SetText("List Raid Wait")
		list = SNFDKP_GetRaidMembers(raidedit:GetText(), "EndList")
	elseif self:GetText() == "List Raid Wait" then
		self:SetText("List Raid Start")
		list = SNFDKP_GetRaidMembers(raidedit:GetText(), "WaitList")
		list2 = SNFDKP_GetRaidMembers(raidedit:GetText(), "LateWait")
	else --failsafe
		self:SetText("List Raid Start")
		return
	end
	if list then
		for _, val in pairs(list) do
			if val then
				local link = "|Hplayer:"..val.."|h["..val.."]|h"
				editlistwindow:Insert(link.."|n")
			end
		end
	end
--[[	if list2 then
		editlistwindow:Insert("------------")
		for _, val in pairs(list2) do
				local link = "|Hplayer:"..val.."|h"..val.."|h"
				editlistwindow:Insert(link.."|n")
			end
		end
	end]]
end